package org.skydiveapp2;

import org.skydiveapp2.ui.InterfaceManager;
import org.skydiveapplibrary.repository.LocationRepository;
import org.skydiveapplibrary.ui.LocationSelectListAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class LocationSelectListActivity extends BaseListActivity
{
	@Override
	public void onCreate(Bundle savedInstanceState)
	{ 
		super.onCreate(savedInstanceState);
		
		overridePendingTransition(R.anim.fadein,
				R.anim.hold);
		
		// init view
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);

		if (this.findViewById(R.id.tabletLayout) != null)
		{
			InterfaceManager.getInstance().configureTabletUI(this, 
					R.string.select_location_label);
		}
		else
		{
			InterfaceManager.getInstance().configurePhoneUI(this, 
					R.string.select_location_label);
		}
	}
	
	@Override
	public void onResume()
	{
		super.onResume();
		
		loadData();
	}
	
	@Override
	protected void onListItemClick(ListView listView, View view, int indx, long id)
	{
		// get current selection
    	int previousSelectedId = getIntent().getIntExtra(Constants.EXTRA_LOCATION_ID, -1);
    	int newSelectedId = (int)id;
    	
    	// create new intent
    	Intent intent =  new Intent();
    	
    	// if different than previous
    	if (previousSelectedId != newSelectedId)
    		intent.putExtra(Constants.EXTRA_LOCATION_ID, newSelectedId);

    	// set result, finish
		setResult(RESULT_OK, intent);
		close();
	}
	
	private void loadData()
	{
		// populate
		LocationRepository repository = new LocationRepository(this);
		LocationSelectListAdapter adapter = new LocationSelectListAdapter(
				this,
				repository.getLocations());
		setListAdapter(adapter);
		
		// set initial selection
		int selectedId = getIntent().getIntExtra(Constants.EXTRA_LOCATION_ID, -1);
		for (int index = 0; index < adapter.getCount(); index++)
		{
			getListView().setItemChecked(index,
					adapter.getItems().get(index).getId() == selectedId); 
		}
	}
	
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
    	MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_add, menu);
        inflater.inflate(R.menu.menu_back, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        // Handle item selection
        switch (item.getItemId())
        {
        case R.id.menu_add:
        	Intent intent = new Intent(this, LocationEditActivity.class);
        	startActivity(intent);
            return true;
            
        case R.id.menu_back:
        	close();
            return true;
        
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
	@Override
    public void onBackPressed() {
		close();
    }
	
	private void close()
	{
		finish();
    	overridePendingTransition(R.anim.hold,
                R.anim.fadeout);
	}
} 